#include <iostream>
#include <string>
#include <boost/json.hpp>
#include <fstream>
#include <exception>
#include <memory>
int main(int argc, char const *argv[])
{
    try
    {
        boost::json::stream_parser fileparser;
        unsigned int file_len = 0;
        std::unique_ptr<char[]> file_buffer;
        std::ifstream ifs("./Test.json", std::ios::ate);
        if (!ifs.is_open())
        {
            throw std::runtime_error("Open File Test.json Error");
        }
        file_len = ifs.tellg();
        ifs.seekg(0, std::ios::beg);
        file_buffer.reset(new char[file_len + 1]);
        file_buffer[file_len] = '\0';
        if (!ifs.read(file_buffer.get(), file_len).good())
        {
            throw std::runtime_error("read Json File Error");
        }
        std::cout << "Buffer Is :" << '\n'
                  << file_buffer.get() << std::endl;
        fileparser.write(file_buffer.get());
        auto localObject = fileparser.release();
        if (!localObject.is_object())
        {
            throw std::runtime_error("Json Error not a Object");
        }
        std::string s(localObject.get_object()["LocalIp"].as_string().begin(), localObject.get_object()["LocalIp"].as_string().end());
        long long localport = localObject.get_object()["LocalPort"].as_int64();
        std::cout << "LocalIp:" << s << " && "
                  << "LocalPort:" << localport << std::endl;
    }
    catch (const std::exception &e)
    {
        std::cerr << e.what() << '\n';
    }
}
